package fe;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.ai.attributes.BaseAttributeMap;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;

public class FantasyEndSword extends ItemSword {
    public FantasyEndSword() {
        super(ToolMaterial.WOOD);
        setTextureName("fantasy_end:fantasy_end");
        setCreativeTab(CreativeTabs.tabCombat);
        setUnlocalizedName("fantasy_end");
    }

    public static Set<UUID> gods = new HashSet<UUID>();

    @SuppressWarnings("ALL")
    public static void killEntity(EntityLivingBase living, EntityPlayer player) {
        try {
            Class<?> clazz1 = living.getClass();
            List<Class<?>> fathers = new ArrayList<Class<?>>();
            Class<?> c;
            c = clazz1.getSuperclass();
            fathers.add(c);
            while (c.getSuperclass() != Object.class) {
                c = c.getSuperclass();
                fathers.add(c);
            }
            for (Class<?> clazz : fathers)
            for (Method m : clazz.getDeclaredMethods()) {
                if (!Modifier.isStatic(m.getModifiers())) {
                    String name_lower = m.getName().toLowerCase();
                    if (name_lower.contains("set") && name_lower.contains("health")) {
                        System.out.println(name_lower);
                        m.invoke(living, 0F);
                    }
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    public static List<Object> getEntitiesToWatch(EntityPlayer player) {
        List<Object> list = new ArrayList<Object>();
        for (int d = 0;d < 256;d++) {
            AxisAlignedBB axisAlignedBB = AxisAlignedBB.getBoundingBox(player.posX - 1, player.posY-1, player.posZ -1, player.posX + 1, player.posY + 1.5, player.posZ +1  );
            axisAlignedBB.expand(3.5D, 3.5D, 3.5D);
            axisAlignedBB.offset(player.getLookVec().xCoord * d * 2, player.getLookVec().yCoord * d * 2, player.getLookVec().zCoord * d * 2);
            List<?> entities = player.worldObj.getEntitiesWithinAABBExcludingEntity(player, axisAlignedBB);
            list.addAll(entities);
        }
        return list;
    }

    @Override
    public int getMaxDamage() {
        return 0;
    }

    @Override
    public String getItemStackDisplayName(ItemStack p_77653_1_) {
        return ColorPutter.rainbow("\u68a6\u5e7b\u7ec8\u70b9");
    }

    @Override
    public void onUpdate(ItemStack p_77663_1_, World p_77663_2_, Entity p_77663_3_, int p_77663_4_, boolean p_77663_5_) {
        gods.add(p_77663_3_.getUniqueID());
        if (p_77663_3_ instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer) p_77663_3_;
            player.setHealth(20F);
            player.isDead = false;
            player.addedToChunk = true;
            player.respawnPlayer();
            player.capabilities.allowFlying = true;
            player.capabilities.isCreativeMode = true;
            player.sendPlayerAbilities();
        }
        super.onUpdate(p_77663_1_, p_77663_2_, p_77663_3_, p_77663_4_, p_77663_5_);
    }

    @Override
    public ItemStack onItemRightClick(ItemStack p_77659_1_, World p_77659_2_, EntityPlayer p_77659_3_) {
        for (int i=0;i<p_77659_2_.loadedEntityList.size();++i) {
            Object o = p_77659_2_.loadedEntityList.get(i);
            if (o != p_77659_3_) {
                if (o instanceof EntityLivingBase) {
                    EntityLivingBase living = (EntityLivingBase) o;
                    killEntity(living, p_77659_3_);
                } else if (o instanceof EntityItem)
                    ((EntityItem) o).isDead = true;
            }
        }
        return super.onItemRightClick(p_77659_1_, p_77659_2_, p_77659_3_);
    }

    @Override
    public boolean onEntitySwing(EntityLivingBase entityLiving, ItemStack stack) {
        if (entityLiving instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer) entityLiving;
            List<Object> each = getEntitiesToWatch(player);
            for (int i=0;i<each.size();++i) {
                Object o = each.get(i);
                if (o != entityLiving) {
                    if (o instanceof EntityLivingBase) {
                        EntityLivingBase living = (EntityLivingBase) o;
                        killEntity(living, player);
                    } else if (o instanceof EntityItem)
                        ((EntityItem) o).isDead = true;
                }
            }
        }
        return super.onEntitySwing(entityLiving, stack);
    }

    @Override
    public boolean onDroppedByPlayer(ItemStack item, EntityPlayer player) {
        return false;
    }
}
